<?php
function get_pagelink($link, $page, $post_count, $perpage, $totalpages = null) {
    if (empty($totalpages)) {
        $totalpages = ceil($post_count/$perpage);
        if (empty($totalpages)) {
            return null;
        }
    }
    $pagelink = '';
    $link = rtrim($link, '/');

    $page_new    = $page - 1;
    $page_old    = $page + 1;
//    $page_start    = $page * $perpage - $perpage + 1;
    $page_start    = ($page - 1) * $perpage + 1;
    $page_end    = $page * $perpage;
    if( $page_end > $post_count )
    {
        $page_end = $post_count;
    }
    
    if( $post_count <= $perpage )
    {
        $pagelink = sprintf(__("<b>1</b> - <b>%s</b> of <b>%s</b>"), $post_count, $post_count);
    }elseif( $page == 1 )
    {
        $pagelink = sprintf(__("<b>%s</b> - <b>%s</b> of <b>%s</b>"), $page_start, $perpage, $post_count) . " <a href=\"$link/2\">" . __('Older') . ' ›</a>';
        if( $totalpages >= 3 )
            $pagelink .= " <a href=\"$link/$totalpages\">" . __('Oldest') . ' »</a> ';
    }elseif( $page == $totalpages )
    {
        
        $pagelink = " <a href=\"$link/$page_new\">‹ " . __('Newer') . "</a> " . sprintf(__("<b>%s</b> - <b>%s</b> of <b>%s</b>"), $page_start, $page_end, $post_count);
        if( $totalpages >= 3 )
            $pagelink = " <a href=\"$link\">« " . __('Newest') . "</a> " . $pagelink;
    }else
    {

        if( $page >= 3 )
            $pagelink = " <a href=\"$link\">« " . __('Newest') . "</a> " . $pagelink;
            
        $pagelink .= " <a href=\"$link/$page_new\">‹ " . __('Newer') . "</a> " . sprintf(__("<b>%s</b> - <b>%s</b> of <b>%s</b>"), $page_start, $page_end, $post_count);
        $pagelink .= " <a href=\"$link/$page_old\">" . __('Older') . ' ›</a>';
        
        if( $totalpages - $page >= 2 )
            $pagelink .= " <a href=\"$link/$totalpages\">" . __('Oldest') . " »</a> ";

    }
    return $pagelink;
}

?>
